/**
 * 明细账
 */
$(function() {
	// 根据条件查看明细账
	$("#search")
			.click(
					function() {
						var captionId = $("#ac").combobox('getValue');
						var date1 = $("#date1").combobox('getValue');
						
						if (captionId == "" || date1 == "") {
							$.messager.alert("提示", "请输入完整的查询条件！", "error");
							return;
						} else {
							$.ajax({
								url : easyExt.url
										+ '/subsidiaryLedger/get?captionId='
										+ captionId + "&date1=" + date1,
								dataType : 'json',
								success : function(data) {
									if (data.success == false) {
										$('#dg').find("tbody").empty();
										$.messager.alert("提示",
												data.msg, "error");
									} else {
										$('#dg').find("tbody").empty();
										// 判断期初余额是否为0，为0即显示“平”，否则不变
										if (data[0].openingBalance == 0) {
											balanceDirection = "平";
										} else {
											balanceDirection = data[0].balanceDirection;
										}
										// 添加期初余额行
										$('#dg').find("tbody").append("<tr><td>"+ data[0].date
																+ "</td><td></td><td>期初余额</td><td></td><td></td><td>"
																+ balanceDirection+ "</td><td>"+ data[0].openingBalance
																+ "</td></tr>");
										//添加凭证明细
										balance = Number(data[0].openingBalance);
										for ( var o in data[1]) {
											// 判断计算后的余额，显示相应的方向，余额取绝对值
											if (data[0].balanceDirection == "借") {
												var a=easyExt.accAdd(Number(balance),Number(data[1][o].debitAmount));
					    						balance=easyExt.accSub(a,Number(data[1][o].creditAmount));
//												balance = balance + Number(data[1][o].debitAmount) - Number(data[1][o].creditAmount);
												if (Number(balance) == 0) {
													balanceDirection = "平";
												} else if (Number(balance) > 0) {
													balanceDirection = "借";
												} else if (Number(balance) < 0) {
													balance = Math.abs(Number(balance));
													balanceDirection = "贷";
												}
											} else {
												var a=easyExt.accAdd(Number(balance),Number(data[1][o].creditAmount));
					    						balance=easyExt.accSub(a,Number(data[1][o].debitAmount));
//												balance = balance - Number(data[1][o].debitAmount) + Number(data[1][o].creditAmount);
												if (Number(balance) == 0) {
													balanceDirection = "平";
												} else if (Number(balance) > 0) {
													balanceDirection = "贷";
												} else if (Number(balance) < 0) {
													balance = Math.abs(Number(balance));
													balanceDirection = "借";
												}
											}
											
											$('#dg').find("tbody").append("<tr><td>"+ data[1][o].date
													+ "</td><td>记字 "+data[1][o].voucherId+" 号</td><td>"+data[1][o].summary+"</td><td>"+
													data[1][o].debitAmount+"</td><td>"+ data[1][o].creditAmount+ "</td><td>"+balanceDirection+"</td><td>"+
													balance+"</td></tr>");
										}
										//添加本月合计、本年累计这两行
										$('#dg').find("tbody").append("<tr><td>"+ data[0].date
										+ "</td><td><td>本月合计</td><td>"+ data[0].jffs
										+ "</td><td>"+ data[0].dffs+ "</td><td>"+ balanceDirection+ "</td><td>"
										+ balance+ "</td></tr>"+ "<tr><td>"+ data[0].date
										+ "</td><td></td><td>本年累计</td><td>"+ data[0].ljjf+ "</td><td>"+ data[0].ljdf
										+ "</td><td>"+ balanceDirection+ "</td><td>"+ balance+ "</td></tr>");
									}
								},error : function() {
								$.messager.alert('提示', "错误",
										"error");
							}
						});
						}
					});
});